﻿app.controller('AccountController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetMessagePageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.MyMessageList = [];
        $http({
            method: 'POST',
            url: '../Account/GetMyAllMessage',
            data: { "SearchText": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.Page = ret.Data;
                    $scope.MyMessageList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.ReadMessage = function (model,searchmodel,page) {
        $scope.MessageDetail = model;
        $http({
            method: 'POST',
            url: '../Account/ReadMessage',
            data: { "Id": model.Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.GetMessagePageList(searchmodel,page);
                }
                else {

                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.ResetPassword = function (model) {
        if (model.NewPassword != model.RePassword) {
            alertify.alert("两次密码不一样，请重新输入");
            return false;
        }
        if (model.NewPassword == null || model.NewPassword=="") {
            alertify.alert("新密码不能为空");
            return false;
        }
        if (model.Password == null || model.Password == "") {
            alertify.alert("原密码不能为空");
            return false;
        }
        if (model.NewPassword.length<6) {
            alertify.alert("新密码长度不能小于6位。");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Account/ModifyPassword',
            data: { "Password": model.Password, "NewPassword": model.NewPassword}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    alertify.alert("密码修改成功,请重新登录");
                    setTimeout(function () {
                        $http.post('../Account/Logout')
                            .success(function (ret) {
                                sessionStorage.removeItem("AccountInfo");
                                sessionStorage.removeItem("LeftMenu");
                                $window.location.href = $window.location.href;
                            })
                            .error(function (data, status, headers, config) {

                            });
                    }, 3000);
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.ModifyPersonalInfo = function (model) {
        if (model.Email == null || model.Email == "") {
            alertify.alert("请填写邮箱信息.");
            return false;
        }
        if (model.Phone == null || model.Phone == "") {
            alertify.alert("请填写联系方式.");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Account/ModifyMyInfo',
            data: { "Email": model.Email, "Phone": model.Phone }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    alertify.alert("个人信息修改成功.");
                   
                    setTimeout(function () {
                        sessionStorage.removeItem("AccountInfo");
                        $window.location.href = $window.location.href;
                    }, 3000);

                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
});